﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Moon.Common;

public partial class Admin_PermissionsManage_RoleManage : System.Web.UI.UserControl
{
    GPI.Server.RoleService serverRole = new GPI.Server.RoleService();
    GPI.Server.AccessService serverAccess = new GPI.Server.AccessService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRoleList();
        }
    }


    /// <summary>
    /// 新建角色
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnCreaterole_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
        this.btnSaveAccess.CommandArgument = "0";
        BindSystemMoudel();
    }
    /// <summary>
    /// 列表方法
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repRoleList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
         int roleid=e.CommandArgument.ObjectToInt(0);
        switch (e.CommandName)
        {
            case "edit":
               
                DataSet ds=serverAccess.GetList("roleID=" + roleid);
                BindSystemMoudel();
                UpdateBind(ds);
                Model.RoleInfo model = serverRole.GetModel(roleid);
                this.txtRoleName.Text = model.roleName;
                this.txtRamrk.Text = model.remark;
                this.btnSaveAccess.CommandArgument = model.ID.ToString();
                this.MultiView1.ActiveViewIndex = 1;
                break;
            case "delete":
                serverRole.Delete(roleid);
                serverAccess.DeleteFromStr( roleid);
                MessageBox.Show(this.Page,"操作成功！");
                BindRoleList();
                break;

        }
    }

    #region 类型方法
    /// <summary> 
    /// 绑定列表方法
    /// </summary>
    private void BindSystemMoudel()
    {
        //权限绑定
        this.cblRoleList.DataSource = new GPI.Server.SystemMoudelInfoService().GetList("ParentID<>0").Tables[0];
        this.cblRoleList.DataBind();
        //模板绑定
        this.cblTemplateList.DataSource = new GPI.Server.TemplateService().GetAllList().Tables[0];
        this.cblTemplateList.DataBind();
        //绑定节点权限
        this.cblNodeList.DataSource = new GPI.Server.NodeService().GetAllList().Tables[0];
        this.cblNodeList.DataBind();

    }
    /// <summary>
    /// 绑定角色列表
    /// </summary>
    private void BindRoleList()
    {
        this.MultiView1.ActiveViewIndex = 0;
        this.repRoleList.DataSource = serverRole.GetList("").Tables[0];
        this.repRoleList.DataBind();
    }

    private void UpdateBind(DataSet ds)
    {
        //获取系统模块权限
        foreach (DataRow dr in ds.Tables[0].Select("accessType=0"))
        {
            if(dr["moduleID"]!=null){
            this.cblRoleList.Items.FindByValue(dr["moduleID"].ToString()).Selected=true;

            }
        }
        //获取模板权限
        foreach (DataRow dr in ds.Tables[0].Select("accessType=1"))
        {
            if (dr["moduleID"] != null)
            {
                this.cblTemplateList.Items.FindByValue(dr["moduleID"].ToString()).Selected = true;

            }
        }
        //获取节点权限
        foreach (DataRow dr in ds.Tables[0].Select("accessType=2"))
        {
            if (dr["moduleID"] != null)
            {
                this.cblNodeList.Items.FindByValue(dr["moduleID"].ToString()).Selected = true;

            }
        }
    }
    #endregion
    /// <summary>
    /// 保存权限信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveAccess_Click(object sender, EventArgs e)
    {
        Model.RoleInfo model = new Model.RoleInfo();
        model.ID=this.btnSaveAccess.CommandArgument.ToInt(0);
        model.roleName = this.txtRoleName.Text;
        model.remark = this.txtRamrk.Text;
        bool isTrue = false;
        if (model.ID == 0)
        {
            isTrue=serverRole.Add(ref model);
            for (int i = 0; i < this.cblRoleList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblRoleList.Items[i].Value.ToInt(0),
                    accessType = 0,
                    roleID = model.ID
                });
            }

            for (int i = 0; i < this.cblTemplateList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblTemplateList.Items[i].Value.ToInt(0),
                    accessType=1,
                    roleID = model.ID
                });
            }

            for (int i = 0; i < this.cblNodeList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblNodeList.Items[i].Value.ToInt(0),
                    accessType=2,
                    roleID = model.ID
                });
            }

        }
        else
        {
            isTrue = serverRole.Update(ref model);
            serverAccess.DeleteFromStr(model.ID);
             for (int i = 0; i < this.cblRoleList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblRoleList.Items[i].Value.ToInt(0),
                    accessType = 0,
                    roleID = model.ID
                });
            }

            for (int i = 0; i < this.cblTemplateList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblTemplateList.Items[i].Value.ToInt(0),
                    accessType = 1,
                    roleID = model.ID
                });
            }

            for (int i = 0; i < this.cblNodeList.Items.Count; i++)
            {
                serverAccess.Add(new Model.AccessInfo()
                {
                    moduleID = this.cblNodeList.Items[i].Value.ToInt(0),
                    accessType = 2,
                    roleID = model.ID
                });
            }

        }
        if (isTrue)
            MessageBox.Show(this.Page, "操作成功！");
        else
            MessageBox.Show(this.Page, "操作失敗！");
        BindRoleList();

    }
    protected void btnReturnList_Click(object sender, EventArgs e)
    {

        BindRoleList();
    }
}
